package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DataAccessObject;

public class UserEditor extends AutoTeamDialog
{
	private static final long serialVersionUID = 5473158398510812466L;

	private JButton changeButton = new JButton("Change");
	private JButton closeButton = new JButton("Cancel");

	private AutoTeamTableModel model;
	private int row;
	
	private String origUsername;
	private String origFirstname;
	private String origLastname;

	private JTextField firstNameField;
	private JTextField lastNameField;
	private JTextField userNameField;
	
	private JComboBox roleBox;
	
	
	public UserEditor(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		super(dialogOwner);
		
		this.model = model;
		this.row = row;
		
		this.setTitle("Edit User");
		this.setDialogSize(new Dimension(300, 250));

		JPanel buttonPane = new JPanel();
		buttonPane.add(changeButton);
		buttonPane.add(closeButton);
		setButtonActions(this);

		JPanel formPane = new JPanel(new GridLayout(7, 2));
		JLabel firstName = new JLabel("First Name: ");
		JLabel lastName = new JLabel("Last Name: ");
		JLabel userName = new JLabel("Username: ");
		JLabel currentPassword = new JLabel("Current Password: ");
		JLabel newPassword = new JLabel("New Password: ");
		JLabel rePassword = new JLabel("Retype Password: ");

		firstNameField = new JTextField(25);
		lastNameField = new JTextField(25);
		userNameField = new JTextField(25);
		JPasswordField currentPasswordField = new JPasswordField(25);
		JPasswordField newPasswordField = new JPasswordField(25);
		JPasswordField rePasswordField = new JPasswordField(25);
        JLabel roleLabel = new JLabel( "Role: ");
        
        String[] roleArray = {"Admin", "Athlete", "Fan"};
        
        roleBox = new JComboBox(roleArray);
		
		// TODO Use enriqueDBmagic to make these textFields autoPopulate
		// userNameField.setText( "dataFromTheDB" );
		origUsername = (String) model.getValueAt(row, 1);
		origFirstname = (String) model.getValueAt(row, 3);
		origLastname = (String) model.getValueAt(row, 4);
		userNameField.setText(origUsername);
		firstNameField.setText(origFirstname);
		lastNameField.setText(origLastname);
		
		userNameField.setEditable(false);

		formPane.add(firstName);
		formPane.add(firstNameField);

		formPane.add(lastName);
		formPane.add(lastNameField);

		formPane.add(userName);
		formPane.add(userNameField);

        formPane.add( roleLabel );
        formPane.add( roleBox );
		
		formPane.add(currentPassword);
		formPane.add(currentPasswordField);

		formPane.add(newPassword);
		formPane.add(newPasswordField);

		formPane.add(rePassword);
		formPane.add(rePasswordField);

		Container contentPane = getContentPane();
		contentPane.add(formPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		pack();
	}

	@Override
	protected void setButtonActions(final AutoTeamDialog thisDialog)
	{
		changeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				//TODO: update database
				DataAccessObject dao = new DataAccessObject();
				ColumnData[] identifiers = { new ColumnData("username",origUsername) };
				ColumnData[] changes = { new ColumnData("username",userNameField.getText()),
										new ColumnData("firstname",firstNameField.getText()),
										new ColumnData("lastname",lastNameField.getText()),
										new ColumnData("role", roleBox.getSelectedIndex())
				};
				dao.updateRecord("users", identifiers, changes);
				model.fireTableDataChanged();
				thisDialog.close();
			}
		});

		closeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				thisDialog.close();
			}
		});
	}
}
